package com.hc.window.settingView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Ping {
	public static void main(String[] args) {
		System.out.println(pingIP("192.168.0.120"));
	}

	public static boolean pingIP(String IP) {
		BufferedReader in = null;
		Runtime rt = Runtime.getRuntime();
		boolean FoundMatch = false;
		String pingCommand = "ping " + IP + " -w " + 100;
		try {
			Process pro = rt.exec(pingCommand);
			in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
			String line = in.readLine();
			while (line != null) {
				try {
					Pattern Regex = Pattern.compile("(T|t){2}(L|l)",
							Pattern.CANON_EQ);
					Matcher RegexMatcher = Regex.matcher(line);
					FoundMatch = RegexMatcher.find();
					if (FoundMatch) {
						pro.destroy();
						return true;
					}
				} catch (PatternSyntaxException ex) {
					ex.getMessage();
				}
				line = in.readLine();
			}
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return false;
	}
}
